﻿using AutoMapper;
using Dm;
using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using ProcurementManagement.Applcation.Command.ProcureEnquiryCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;
using System.Collections.Generic;
using static ERPPublic.ParseAPI;

namespace ProcurementManagement.Applcation.CommandHanlers.ProcureEnquiryCommandHandlers
{
    /// <summary>
    /// 采购询价反填
    /// </summary>
    public class ProcureEnquirySetCommandHandlers : IRequestHandler<ProcureEnquirySetCommand, ProcureEnquirySetDto>
    {
        protected readonly IProcureEnquiryRepository repository;
        protected readonly IInquiryVendorRepository vendorRepository;
        protected readonly ISqlSugarClient db;

        public ProcureEnquirySetCommandHandlers(IProcureEnquiryRepository repository, IInquiryVendorRepository vendorRepository, ISqlSugarClient db)
        {
            this.repository = repository;
            this.vendorRepository = vendorRepository;
            this.db = db;
        }



        public async Task<ProcureEnquirySetDto> Handle(ProcureEnquirySetCommand request, CancellationToken cancellationToken)
        {

            try
            {
                Products pros = await GetProductList("", "", 1, 100, request.id);
                var list = (await repository.GetAll()).First(x => x.ProcureEnquiryId == request.id);
                ProcureEnquirySetDto dtos = new ProcureEnquirySetDto();
                dtos.ProcureEnquiryId = list.ProcureEnquiryId;
                dtos.PEDocumentsNumber = list.PEDocumentsNumber;
                dtos.PEPricingPeople = list.PEPricingPeople;
                dtos.PEInquiryClerk = list.PEInquiryClerk;
                dtos.PEInquiryDate = list.PEInquiryDate;
                dtos.ProductId = list.ProductId;
                dtos.PName = pros.PName;
                dtos.PNumber = pros.PNumber;
                dtos.PENum = list.PENum;
                dtos.PEPrice = list.PEPrice;
                dtos.PETaxRate = list.PETaxRate;
                dtos.AddName = list.AddName;
                dtos.AddTime = list.AddTime;
                dtos.Office_Id = list.Office_Id;
                dtos.User_Id = list.User_Id;
                dtos.IsState = list.IsState;

                var vendors = (await vendorRepository.GetAll()).Where(x => x.PEDocumentsNumber == list.PEDocumentsNumber).Select(x => new InquiryVendorDtos
                {
                    VName = x.VendorName,
                    rPNum = x.VendorNum,
                    rpPrice = x.VendorPrice,
                    SumPrice = x.SumPrice,
                    date = x.DeliveryDate,
                    AddName = x.AddName,
                    AddTime = x.AddTime,
                    Office_Id = x.Office_Id,
                    User_Id = x.User_Id
                }).ToList();
                dtos.dtos = vendors;

                return dtos;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductList(string? PName, string? PNumber, int pindex, int psize, int id)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/erpcp/api/Product/ProductShow?PName=" + PName + "&PNumber=" + PNumber + "&ProductTypeId=" + 0 + "&PageIndex=" + pindex + "&PageSize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            var newlist = (await repository.GetAll()).First(x => x.ProcureEnquiryId == id);
            return json.data.list.First(x => x.ProductId == newlist.ProductId);
        }


        //产品
        public class Products
        {
            /// <summary>
            /// 产品Id
            /// </summary>
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
            public int ProductId { get; set; }
            /// <summary>
            /// 标识
            /// </summary>

            public int? PCharacteristic { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PNumber { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PName { get; set; }
            /// <summary>
            /// 规格型号
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PSpec { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PUnit { get; set; }

            /// <summary>
            /// 产品分类ID
            /// </summary>

            public int ProductTypeId { get; set; }




        }
    }
}
